﻿@{
    ViewBag.Title = "Form"; 
    Layout = "~/Views/Shared/_Form.cshtml";
}
    <script>
        layui.use(['jquery', 'table', 'form', 'commonTable', 'common', 'optimizeSelectOption'], function () {
            var form = layui.form,
                $ = layui.$,
                common = layui.common,
                table = layui.table,
                commonTable = layui.commonTable;
            var keyValue = $.request("keyValue");
            if (!!keyValue) {
                $('#F_ModuleId').prop('disabled', true);
            }
            //权限字段
            common.authorizeFields('adminform');
            $(function () {
                initControl();
                if (!!keyValue) {
                    common.ajax({
                        url: '/SystemOrganize/DataPrivilegeRule/GetFormJson',
                        dataType: 'json',
                        data: { keyValue: keyValue },
                        async: false,
                        success: function (data) {
                            common.val('adminform', data);
                            var datas = JSON.parse(data.F_PrivilegeRules);
                            for (var i = 0; i < datas.length; i++) {
                                datas[i]["F_Id"] = uuid();
                            }
                            table.reload('currentTableId', {
                                data: datas
                            });
                        }
                    });
                }
                else {
                    if (!$('#F_ModuleId').val()) {
                        $('#toolbar').addClass('layui-hide');
                    }
                }
                form.render();
            });
            wcLoading.close();
            commonTable.rendertable({
                elem: '#currentTableId',
                id: 'currentTableId',
                data: [],
                search:false,
                limit: 9999,//每页数据 默认
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['count'] //自定义分页布局
                    , first: false //不显示首页
                    , last: false //不显示尾页
                },
                height: 'full-150',
                cols: [[
                    { type: "radio", width: 50 },
                    { field: 'Operation', title: '规则间条件', width: 120 },
                    { field: 'Filters', title: '规则内容', minWidth: 200 },
                    { field: 'Description', title: '规则备注', minWidth: 200 },
                    { title: '操作', width: 130, toolbar: '#currentTableBar', align: "center" }
                ]]
            });
            //行点击事件监听，控制按钮显示
            var oneList = ["NF-edit", "NF-delete"];//选择1条显示
            commonTable.tableRowClick("radio", "currentTableFilter", "currentTableId", oneList);
            //toolbar监听事件
            table.on('toolbar(currentTableFilter)', function (obj) {
                var data = table.checkStatus('currentTableId').data;
                var id = data.length > 0 ? data[0].F_Id : null;
                var module = $('#F_ModuleId').val();
                if (obj.event === 'add') {  // 监听删除操作
                    if (!module) {
                        common.modalMsg("未选中模块", "warning");
                        return false;
                    }
                    common.modalOpen({
                        title: "添加规则",
                        url: "/SystemOrganize/DataPrivilegeRule/RuleForm?module=" + module,
                        width: "750px",
                        height: "500px",
                    });
                }
                else if (obj.event === 'delete') {
                    if (id == null) {
                        common.modalMsg("未选中数据", "warning");
                        return false;
                    }
                    common.modalConfirm("注：您确定要删除该项数据吗？", function (r) {
                        if (r) {
                            var index = parent.layer.load(0, {
                                shade: [0.5, '#000'], //0.1透明度的背景
                            });
                            try {
                                var cachedata = table.cache.currentTableId;
                                for (var i = 0; i < cachedata.length; i++) {
                                    if (id == cachedata[i].F_Id) {
                                        cachedata.splice(i, 1);
                                        break;
                                    }
                                }
                                table.reload('currentTableId', {
                                    data: cachedata
                                });
                                entity = null;
                                common.modalMsg("操作成功", "success");
                            } catch (e) {
                                alert(e);
                            }
                            parent.layer.close(index);
                        }
                        else {
                            return false;
                        }
                    });
                }
                else if (obj.event === 'edit') {
                    if (id == null) {
                        common.modalMsg("未选中数据", "warning");
                        return false;
                    }
                    if (!module) {
                        common.modalMsg("未选中模块", "warning");
                        return false;
                    }
                    common.modalOpen({
                        title: "编辑规则",
                        url: "/SystemOrganize/DataPrivilegeRule/RuleForm?keyValue=" + id + "&module=" + module,
                        width: "750px",
                        height: "500px",
                    });
                }
                return false;
            });
            //toolrow监听事件
            table.on('tool(currentTableFilter)', function (obj) {
                var id = obj.data.F_Id;
                var module = $('#F_ModuleId').val();
                if (obj.event === 'delete') {
                    common.modalConfirm("注：您确定要删除该项数据吗？", function (r) {
                        if (r) {
                            var index = parent.layer.load(0, {
                                shade: [0.5, '#000'], //0.1透明度的背景
                            });
                            try {
                                var cachedata = table.cache.currentTableId;
                                for (var i = 0; i < cachedata.length; i++) {
                                    if (id == cachedata[i].F_Id) {
                                        cachedata.splice(i, 1);
                                        break;
                                    }
                                }
                                table.reload('currentTableId', {
                                    data: cachedata
                                });
                                entity = null;
                                common.modalMsg("操作成功", "success");
                            } catch (e) {
                                alert(e);
                            }
                            parent.layer.close(index);
                        }
                        else {
                            return false;
                        }
                    });
                }
                else if (obj.event === 'edit') {
                    if (!module) {
                        common.modalMsg("未选中模块", "warning");
                        return false;
                    }
                    common.modalOpen({
                        title: "编辑规则",
                        url: "/SystemOrganize/DataPrivilegeRule/RuleForm?keyValue=" + id + "&module=" + module,
                        width: "750px",
                        height: "500px",
                    });
                }
                return false;
            });
            function initControl() {
                //绑定数据源
                //类型为下拉框时
                if (!!keyValue) {
                    $("#F_ModuleId").bindSelect({
                        url: "/SystemManage/Module/GetSelectMunuJson",
                    });
                }
                else {
                    $("#F_ModuleId").bindSelect({
                        url: "/SystemManage/Module/GetSelectMunuBesidesJson",
                    });
                }

            }
            //select验证
            form.verify({
                required: function (value, item) {
                    var msg = "必填项不能为空";
                    value = $.trim(value);
                    var isEmpty = !value || value.length < 1;
                    // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                    if (item.tagName == 'SELECT' && isEmpty) {
                        $("html").animate({
                            scrollTop: $(item).siblings(".layui-form-select").offset().top - 74
                        }, 50);
                    }
                    if (isEmpty) {
                        return msg;
                    }
                }
            });
            //监听select
            form.on('select(F_ModuleId)', function (data) {
                if (!!data.value) {
                    $('#toolbar').removeClass('layui-hide');
                }
                else {
                    $('#toolbar').addClass('layui-hide');
                }
                return false;
            });
            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var cachedata = table.cache.currentTableId
                if (cachedata.length == 0) {
                    common.modalMsg("未添加规则", "warning");
                    return false;
                }
                var postData = data.field;
                postData.listData = JSON.stringify(cachedata);
                common.submitForm({
                    url: '/SystemOrganize/DataPrivilegeRule/SubmitForm?keyValue=' + keyValue,
                    param: postData,
                    success: function () {
                        common.reloadIframe("/SystemOrganize/DataPrivilegeRule/Index", 'data-search-btn');
                    }
                })
                return false;
            });
        });
    </script>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <fieldset class="table-search-fieldset">
                <div class="layui-form layuimini-form" lay-filter="adminform">
                    <div class="layui-form-item">
                        <div class="layui-col-md4 layui-hide">
                            <label class="layui-form-label required">模块编号</label>
                            <div class="layui-input-block">
                                <select id="F_ModuleId" name="F_ModuleId" lay-filter="F_ModuleId" lay-verify="required" lay-search>
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-col-md4 layui-hide">
                            <label class="layui-form-label required">排序</label>
                            <div class="layui-input-block">
                                <input type="number" pattern="[0-9]*" id="F_SortCode" name="F_SortCode" lay-verify="required|number" oninput="if(value.length>8)value=value.slice(0,8)" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-col-md4 layui-hide">
                            <label class="layui-form-label">选项</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="F_EnabledMark" id="F_EnabledMark" checked="" value="true" title="有效标识">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item text-right">
                        <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </fieldset>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container" id="toolbar">
                    <button id="NF-add" authorize="yes" class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新建</button>
                    <button id="NF-edit" name="NF-edit" authorize="yes" class="layui-btn layui-btn-sm layui-btn-warm data-edit-btn layui-hide" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</button>
                    <button id="NF-delete" name="NF-delete" authorize="yes" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
                </div>
            </script>
            <script type="text/html" id="currentTableBar">
                <a id="NF-edit" authorize class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
                <a id="NF-delete" authorize class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"><i class="layui-icon">&#xe640;</i></a>
            </script>
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </div>
    </div>
</body>

